//
//  CameraViewController.swift
//  驻外机组
//
//  Created by 苏伟 on 16/3/2.
//  Copyright © 2016年 com.csair. All rights reserved.
//

import UIKit

class CameraViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    var imgView = UIImageView()
    var img = UIImage()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let btn = UIButton()
        btn.frame = CGRectMake(50, 120, 200, 40)
        btn.backgroundColor = UIColor.orangeColor()
        btn.addTarget(self, action: "goCamera", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
        let btn1 = UIButton()
        btn1.frame = CGRectMake(50, 200, 200, 40)
        btn1.backgroundColor = UIColor.redColor()
    
        btn1.addTarget(self, action: "goImage", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(btn1)
        imgView.frame = CGRectMake(100, 260, 100, 100)
        self.view.addSubview(imgView)
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
  
    
    //打开相机
    func goCamera(){
    //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
        var sourceType = UIImagePickerControllerSourceType.Camera
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        }
    
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.allowsEditing = true//设置可编辑
        picker.sourceType = sourceType
        self.presentViewController(picker, animated: true, completion: nil)//进入照相界面
    }
  
    
    
    func goImage(){
        let pickerImage = UIImagePickerController()
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
            pickerImage.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            pickerImage.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(pickerImage.sourceType)!
        }
        pickerImage.delegate = self
        pickerImage.allowsEditing = true
        self.presentViewController(pickerImage, animated: true, completion: nil)
    }
    
    
    //选择好照片后choose后执行的方法
//    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
//    
//        print("choose--------->>")
//        print(info)
//    
//        img = info[UIImagePickerControllerEditedImage] as! UIImage
//        imgView.image = img
//        picker.dismissViewControllerAnimated(true, completion: nil)
//    }
    
    
    //cancel后执行的方法
    func imagePickerControllerDidCancel(picker: UIImagePickerController){
        print("cancel--------->>")
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
}
